package com.zbfmq.web;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/10  14:01
 * 描述:
 */
@RestController
public class TestMQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("zhilian")
    public void test(){
        rabbitTemplate.convertAndSend("ZHI-LIAN","zhiliankey","hello mq!");
    }


    @RequestMapping("topic/{key}")
    public void topic(@PathVariable String key){
        rabbitTemplate.convertAndSend("TOPIC-1",key,"hello mq topic!");
    }


    @RequestMapping("fanout")
    public void fanout(){
        rabbitTemplate.convertAndSend("FANOUT-1","","hello mq fanout !");
    }


    @RequestMapping("header")
    public void header(){
        MessageProperties messageProperties=new MessageProperties();
        messageProperties.getHeaders().put("zhangsan","123");

        messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);

        Message message=new Message("hello header !".getBytes(),messageProperties);

        rabbitTemplate.send("HEADER-1",null,message);

    }


    @Autowired
    private AmqpAdmin rabbitAdmin;

    @RequestMapping("/add/{name}")
    public void test01(@PathVariable String name){
        String name55=name + 1;
        rabbitAdmin.declareQueue(new Queue(name55));
        DirectExchange directExchange = new DirectExchange(name55);
        rabbitAdmin.declareExchange(directExchange);
        rabbitAdmin.declareBinding(new Binding(name55,null,name55,"TTTT",null));

    }


}
